<template>
  <div class="container">
    <div class="header">
      <el-button
        type="info"
        icon="el-icon-plus"
        @click="handleAction('dish')"
        >新增菜品分类</el-button
      >
      <el-button
        type="primary"
        icon="el-icon-plus"
        @click="handleAction('package')"
        >新增套餐分类</el-button
      >
    </div>

    <el-table :data="tableData" style="width: 100%" border>
      <el-table-column prop="name" label="分类名称"> </el-table-column>
      <el-table-column prop="type" label="分类类型"> </el-table-column>
      <el-table-column prop="createTime" label="操作时间"> </el-table-column>
      <el-table-column prop="sort" label="排序"> </el-table-column>
      <el-table-column label="操作" header-align="center" align="center">
        <template v-slot="{ row }">
          <el-link type="primary">编辑</el-link>
          <el-divider direction="vertical"></el-divider>
          <el-popconfirm
            title="确认调整该账号的状态?"
            @onConfirm="handleChangeStatus(row)"
          >
            <el-link type="danger" slot="reference">{{
              row.status ? "修改" : "启用"
            }}</el-link>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="footer">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageInfo.page"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="pageInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="40%"
      >
      <el-form class="form" ref="form" :model="form" label-width="180px">
        <el-form-item label="分类名称:">
            <el-input style="width: 280px;" class="inputs" v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="排序:">
            <el-input style="width: 280px;" class="inputs" v-model="form.sort"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
        <el-button type="info" @click="">保存并继续添加</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getCategories } from "@/api/category";
export default {
  data() {
    return {
      pageInfo: {
        page: 1,
        pageSize: 10,
      },
      total: 0,
      input: "",
      tableData: [],
      dialogVisible:false,
      title:'',
      form:{
        name:'',
        sort:''
      }
    };
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      getCategories(this.pageInfo).then(res=>{
        this.tableData = res.data.records;
        this.total = res.data.total;
      })
    },
    handleSizeChange(val) {
      this.pageInfo.page = 1;
      this.pageInfo.pageSize = val;
      this.init();
    },
    handleCurrentChange(val) {
      this.pageInfo.page = val;
      this.init();
    },
    handleSearch() {
      this.pageInfo.page = 1;
      this.init();
    },
    handleAction(mes){
      if(mes==='dish'){
        this.title = '新增菜品分类'
      }else{
        this.title = '新增套餐分类'
      }
      this.dialogVisible = true
    }
    // handleChangeStatus(row) {
    //   changeStatus({ id: row.id, status: +!row.status }).then((res) => {
    //     this.$message.success("改变成功");
    //     this.init();
    //   });
    // },
  },
};
</script>

<style lang="scss" scoped>
.container {
  padding: 22px;
  .header {
    // display: flex;
    // justify-content: space-between;
  }
  .el-table {
    margin-top: 20px;
  }
  .footer {
    margin-top: 22px;
    display: flex;
    justify-content: center;
  }
}
</style>
